import com.demo.ArticleController;
import com.demo.BController;
import com.demo.StudentController.SController;
import com.method.Student;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.ClassPathResource;

import javax.xml.bind.annotation.XmlAccessorOrder;
import java.beans.Introspector;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2023-10-26
 * Time: 10:24
 */
public class Test {
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        SController sController = context.getBean("SController", SController.class);
        sController.Say();
//        Student student = context.getBean("s1", Student.class);//多个方法返回同样的类对象，采用覆盖 | 优先的原则
//        System.out.println(student);

        //ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
//        BeanFactory context = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
//        User user = context.getBean("user",User.class);
//        user.Say();

//        String str = "BCController";
//        System.out.println(Introspector.decapitalize(str));
//
//        String str1 = "bCController";
//        System.out.println(Introspector.decapitalize(str1));
//
//        String str2 = "BcController";
//        System.out.println(Introspector.decapitalize(str2));

        //如果类名首字母是小写，就是小写，首字母是大写，第二个字母是大写，就使用原类名
//        BController bController = context.getBean("BController",BController.class);//此时就会找不到
//        System.out.println(bController.Say());

        //ID就是类名首字母小写
//        ArticleController articleController = context.getBean("articleController", ArticleController.class);
//        articleController.Say();


        //1、先获取到spring的上下文，也就是spring对象
        //ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        //System.out.println("ApplicationContext 之后!");//这是一个俄汉模式的加载，提前一次性把所有的bean对象加载出来，再去使用bean对象
        //2、从上下文中得到bean对象 根据id去找对象
        //1.1 User user = (User) context.getBean("user");//通过名称获取bean对象，身份标识，但是如果对象为空，强转就会出现问题
//        User user1 = (User) context.getBean("user1");
//        System.out.println(user);//创建了两份不同的对象
//        System.out.println(user1);
//        System.out.println(user == user1);
        //1.2 User user = context.getBean(User.class);//通过bean类型去获取bean对象，但是如果有多个bean对象就会出现问题(不是唯一的bean异常)
        //1.3 通过id + bean类型获取bean对象

//        BeanFactory context = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
//        System.out.println("BeanFactory 之后！");//没有提前加载，使用哪个bean就获取哪个bean对象
//        User user = context.getBean("user", User.class);//此时不需要强转
//        //3、再使用bean对象
//        user.Say();
    }
}
